<?
ini_set("display_errors",1);
include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');
include($_SERVER['DOCUMENT_ROOT'].'/filter.libs.php');

portal_start();

####################################
#

include($_SERVER['DOCUMENT_ROOT'].'/header.inc.php');





$sql = array();
$sql['columns'] = 'gridimage_id,title,realname,user_id,grid_reference,comment,imagetaken,imageclass';
$sql['tables'] = array();

$sql['tables'][] = "p2.gridimage{$portal_id}_bi_view AS gi";

$sql['wheres'] = array();

$sql['order'] = "created DESC,gridimage_id DESC";



$opt = array();
$opt['view'] = 'listing';
	
#########################
# html form

	print "<h2>Most extreme images in the ".he($row['title'])." Portal</h2>";

print "<form class=\"sform\">";

include($_SERVER['DOCUMENT_ROOT'].'/filter_setup.inc.php');

print "</form>";

#########################

$cols = $sql['columns'];

$sql['limit'] = 1;

$bits = array();

$sql['columns'] = "$cols,'Most Northerly' as break";
$sql['order'] = 'wgs84_lat desc';
$bits[] = sqlMakeQuery($sql);

$sql['columns'] = "$cols,'Most Eastery' as break";
$sql['order'] = 'wgs84_long desc';
$bits[] = sqlMakeQuery($sql);

$sql['columns'] = "$cols,'Most Southerly' as break";
$sql['order'] = 'wgs84_lat asc';
$bits[] = sqlMakeQuery($sql);

$sql['columns'] = "$cols,'Most Westerly' as break";
$sql['order'] = 'wgs84_long asc';
$bits[] = sqlMakeQuery($sql);

if ($portal['linear_start_id'] > 0) {
	$sql['columns'] = "$cols,'Linear Starting image' as break";
	$sql['wheres']['gid'] = "gridimage_id = {$portal['linear_start_id']}";
	unset($sql['order']);
	$bits[] = sqlMakeQuery($sql);
	
	unset($sql['wheres']['gid']);
}

$sql['columns'] = "$cols,'Most Recent' as break";
$sql['order'] = 'imagetaken desc';
$bits[] = sqlMakeQuery($sql);

$sql['columns'] = "$cols,'Oldest' as break";
$sql['wheres']['y'] = "imagetaken > 1";
$sql['order'] = 'imagetaken asc';
$bits[] = sqlMakeQuery($sql);
unset($sql['wheres']['y']);

$sql['columns'] = "$cols,'First Submitted' as break";
$sql['order'] = 'gridimage_id asc';
$bits[] = sqlMakeQuery($sql);

$query = "(".implode(") UNION (",$bits).")";


function searchbreak(&$c,$start='',$end='') {
	if (empty($c)) {//its the last one
		print $end;
		return;
	}

	print $end;

	print "<h4>{$c['break']}</h4>";
	print $start;

	return true; //we dont disable any images...
}

#########################
# Display results!


$result = queryExecute($query);

if ($result) {

	include($_SERVER['DOCUMENT_ROOT']."/views/{$opt['view']}.php");

} else {
	print "<p>Unable to Images to show at this time.</p>";
}
	


include($_SERVER['DOCUMENT_ROOT'].'/footer.inc.php');

